/*
 * Wazuh Vulnerability Scanner - Unit Tests
 * Copyright (C) 2015, Wazuh Inc.
 * February 21, 2024.
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General Public
 * License (version 2) as published by the FSF - Free Software
 * Foundation.
 */

#ifndef _FETCH_ALL_AGENTS_FROM_GLOBAL_DB_HPP
#define _FETCH_ALL_AGENTS_FROM_GLOBAL_DB_HPP

#include "socketServer.hpp"
#include "gtest/gtest.h"

const std::filesystem::path TEST_DB_PATH {std::filesystem::current_path() / "queue/db"};
const std::filesystem::path TEST_SOCKET_PATH {TEST_DB_PATH / "wdb"};
const std::string EXPECTED_QUERY {"global sql SELECT * FROM agent "};
/**
 * @brief FetchFromGlobalDB test class.
 */
class FetchAllAgentsFromGlobalDBTest : public ::testing::Test
{
protected:
    // LCOV_EXCL_START
    FetchAllAgentsFromGlobalDBTest() = default;
    ~FetchAllAgentsFromGlobalDBTest() override = default;

    /**
     * @brief Set the environment for testing.
     *
     */
    void SetUp() override
    {
        std::filesystem::create_directories(TEST_DB_PATH);
        m_socketServer =
            std::make_shared<SocketServer<Socket<OSPrimitives, SizeHeaderProtocol>, EpollWrapper>>(TEST_SOCKET_PATH);

        m_socketServer->listen(
            [&](const int fd, const char* data, uint32_t size, const char* dataHeader, uint32_t sizeHeader)
            {
                std::ignore = dataHeader;
                std::ignore = sizeHeader;

                std::string receivedMsg(data, size);
                EXPECT_STREQ(receivedMsg.c_str(), EXPECTED_QUERY.c_str());

                m_socketServer->send(fd, "ok []", 5);
            });
    }

    /**
     * @brief Clean the environment after testing.
     *
     */
    void TearDown() override
    {
        std::filesystem::remove_all("queue");
        m_socketServer.reset();
    }

    /**
     * @brief Fake server socket.
     */
    std::shared_ptr<SocketServer<Socket<OSPrimitives, SizeHeaderProtocol>, EpollWrapper>> m_socketServer;
    // LCOV_EXCL_STOP
};

#endif // _FETCH_ALL_AGENTS_FROM_GLOBAL_DB_HPP
